package com.forest.gissingle.sql;

import java.util.ArrayList;
import java.util.List;

import com.forest.gissingle.bean.SqlDataBean;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelperTotal extends SQLiteOpenHelper {

	private String TAG = "sptest";

	DBTotalTables totalTables;

	public DBHelperTotal(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	/**
	 * 创建表
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		DBInitTables tables = new DBInitTables();
		// 所要创建表的SQL语句
		List<String> sqls = tables.createTotalTableSqls();
		// for (String sql : sqls) {
		// db.execSQL(sql);
		// }
		execSqls(db, sqls);
		Log.d(TAG, "DBHelperTotalonCreate");
		// initData(db);
	}

	/**
	 * 将封装好的插入、更新或删除的sql语句传入 样例： DBManager.getInstance().execSqls( new String[]
	 * {"insert into mode(name,descript,screen_light) values('a','b','1)"} );
	 * 更新：
	 * "update mode set name='bb',descript='vvvv',screen_light='233' where id=1"
	 * 
	 * @param sqls
	 */
	private void execSqls(SQLiteDatabase database, List<String> sqls) {
		try {
			database.beginTransaction();
			for (String sql : sqls) {
				database.execSQL(sql.toString());
			}

			database.setTransactionSuccessful();
		} catch (Exception e) {
			e.printStackTrace();
			// database.endTransaction();
		} finally {
			database.endTransaction();
			// database.close();
		}
	}

	// /**
	// * 实时记录总流量数据
	// */
	// public void statsTotalTraff() {
	// totalTables = new DBTotalTables();
	// totalTables.initTime();
	// TotalTraffs totalTraff = DBUtils.initTotalData();
	// SQLiteDatabase db = this.getWritableDatabase();
	// db.beginTransaction();
	// try {
	// totalTables.getCurrentData(db, DBManager.MOBILE_TABLE,
	// totalTraff.getMobileUpload(),
	// totalTraff.getMobileDownload());
	// totalTables.getCurrentData(db, DBManager.WIFI_TABLE,
	// totalTraff.getWifiUpload(), totalTraff.getWifiDownload());
	// db.setTransactionSuccessful();
	// } finally {
	// db.endTransaction();
	// }
	// db.close();
	// }

	public void insertSqlData(SqlDataBean sqlBean) {
		totalTables = new DBTotalTables();
		totalTables.initTime();
		SQLiteDatabase db = this.getWritableDatabase();
		db.beginTransaction();
		try {
			totalTables.insertSqlBean(db, sqlBean);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
		db.close();
	}

	public void insertSqlDataArray(ArrayList<SqlDataBean> sqlBeanArray) {
		totalTables = new DBTotalTables();
		totalTables.initTime();
		SQLiteDatabase db = this.getWritableDatabase();
		db.beginTransaction();
		try {
			totalTables.insertSqlBeanArray(db, sqlBeanArray);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
		db.close();
	}

	public SqlDataBean searchDB(int region) {
		SqlDataBean bean = null;
		totalTables = new DBTotalTables();
		totalTables.initTime();
		SQLiteDatabase db = this.getWritableDatabase();
		db.beginTransaction();
		try {
			bean = totalTables.searchDB(db, region);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
		db.close();
		return bean;
	}

	/**
	 * 数据库更新
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

}
